package com.lightjet.macross.biz.portal.dto.service.impl;

import com.lightjet.macross.biz.portal.dto.GraphNewsDTO;
import com.lightjet.macross.biz.portal.dto.service.ContentService;
import com.lightjet.macross.common.dto.ResponseDTO;
import oracle.jdbc.OracleTypes;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

@Service
public class ContentServiceImpl implements ContentService {
    @Resource
    protected JdbcTemplate template;

    @Override
    public List<GraphNewsDTO> GetLatestGraphNews(Integer count) {
        count = count==null?5:count;
        String proc = "{call query_pack.getblockcontentrsbyparentid(?,?,?,?,?,?,?)}";
        RowMapper rowMapper = new BeanPropertyRowMapper(GraphNewsDTO.class);
        Integer finalCount = count;
        List<GraphNewsDTO> retList =  (List<GraphNewsDTO>) template.execute(connection -> {
            CallableStatement cs = connection.prepareCall(proc);
            cs.setString(1, "05461B071E3B6318E0507A0A808004C0");
            cs.setString(2, "-1");
            cs.setFloat(3, -1);
            cs.setString(4, "1");
            cs.setFloat(5, Float.valueOf(finalCount.toString()) );
            cs.setFloat(6, 200);
            cs.registerOutParameter(7, OracleTypes.CURSOR);
            return cs;
        }, (CallableStatementCallback) callableStatement -> {
            List<GraphNewsDTO> list = new ArrayList<>();
            callableStatement.execute();
            ResultSet rs = (ResultSet) callableStatement.getObject(7);
            while (rs.next()){
                GraphNewsDTO item = (GraphNewsDTO) rowMapper.mapRow(rs,rs.getRow());
                list.add(item);
            }
            return list;
        });
        for(GraphNewsDTO item : retList)
        {
            item.setContent("http://www.802.sast.casc/NewWeb802/FileStorage/" +item.getContent().split("\\|")[0]);
        }

        return  retList;
    }
}
